package com.haiyou.dc.io;

import java.io.*;

public class TestSerializable {

    private static String fileName = "E:\\IdeaProjects\\my-java-project\\dc-guava\\src\\main\\java\\com\\haiyou\\dc\\io\\person.txt";

    public static void main(String[] args) throws IOException {
        test1();
        test2();
    }

    /**
     * 序列化对象
     */
    public static void test1() throws IOException {
        Person person = new Person("张三", "男", 30, "huangzl", "广东省广州市");
        try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(fileName));) {
            objectOutputStream.writeObject(person);
        }
    }

    /**
     * 反序列化
     */
    public static void test2() throws IOException {
        try (ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(fileName))) {
            Person person = (Person) objectInputStream.readObject();
            System.out.println(person);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

}


